<?php


namespace app\admin\controller;


use app\common\model\ShopGoodsCategoryModel;
use app\common\model\ShopGoodsModel;
use app\common\model\ShopModel;
use app\common\model\ShopOrderModel;
use app\common\model\ShopWithdrawalsModel;
use think\Db;
use think\facade\Request;

class ShopController extends BaseController {
    public function goods_add() {
        $category = ShopGoodsCategoryModel::where(['status' => 1])->select();
        $this->assign("categorys", $category);
        return $this->fetch();
    }

    public function goods_add_post() {
        $goods              = new ShopGoodsModel(Request::param());
        $goods->create_time = nowFormat();
        if ($goods->save()) {
            // 记录日志
            $log_content = "添加商城商品，商品ID：{$goods->id}，名称：{$goods->title}，价格：{$goods->price}，库存：{$goods->stock}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function goods_edit() {
        $category = ShopGoodsCategoryModel::where(['status' => 1])->select();
        $this->assign("categorys", $category);
        $goods = ShopGoodsModel::where(['id' => Request::param('id')])->find();
        $this->assign("goods", $goods);
        return $this->fetch();
    }

    public function goods_edit_post() {
        $goods = ShopGoodsModel::get(Request::param("id"));
        if (!$goods) {
            return self::bulidFail("商品不存在");
        }
        if ($goods->save(Request::param())) {
            // 记录日志
            $log_content = "修改商城商品，商品ID：{$goods->id}，名称：{$goods->title}，价格：{$goods->price}，库存：{$goods->stock}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }


    public function goods() {
        $categorys = ShopGoodsCategoryModel::select();
        $this->assign('categorys', $categorys);
        return $this->fetch();
    }

    public function getGoodsList() {
        $page  = Request::param('page') ?? 1;
        $limit = Request::param("limit") ?? 20;

        $where = [];

        $id         = Request::param("id");
        $categoryid = Request::param("categoryid");

        if ($id) {
            $where["id"] = $id;
        }
        if ($categoryid) {
            $where["categoryid"] = $categoryid;
        }

        $lists = ShopGoodsModel::where($where)->with(['category'])->limit(($page - 1) * $limit, $limit)->order('create_time', 'desc')->select();
        if (count($lists) > 0) {
            $count = ShopGoodsModel::where($where)->count('id');
            return json(["code" => 0, "msg" => "", "data" => $lists, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    /* ————————————————————————————————————————————————————— 订单管理 —————————————————————————————————————————————————————— */

    public function order() {
        return $this->fetch();
    }

    public function getOrderList() {
        $where          = [];
        $whereTimeStart = '';
        $whereTimeEnd   = '';

        $order_no   = Request::param("order_no");
        $uid        = Request::param('uid');
        $status     = Request::param('status');
        $start_time = Request::param("start_time");
        $end_time   = Request::param("end_time");

        if ($order_no) {
            $where["order_no"] = $order_no;
        }
        if ($uid) {
            $where["uid"] = $uid;
        }
        if ($status) {
            $where["status"] = $status;
        }
        if ($start_time) {
            $whereTimeStart = "create_time > '{$start_time}'";
        }
        if ($end_time) {
            $whereTimeEnd = "create_time < '{$end_time}'";
        }
        $page   = Request::param("page");
        $limit  = Request::param("limit");
        $orders = ShopOrderModel::where($where)->where($whereTimeStart)->where($whereTimeEnd)->limit(($page - 1) * $limit, $limit)->order("create_time", "desc")->select();
        if (count($orders) > 0) {
            $count = ShopOrderModel::where($where)->where($whereTimeStart)->where($whereTimeEnd)->count("id");
            return json(["code" => 0, "msg" => "", "data" => $orders, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    //发货
    public function order_dev() {
        $order = ShopOrderModel::get(Request::param("id"));
        $this->assign("orderinfo", $order);
        return $this->fetch();
    }

    public function order_dev_post() {
        $order                  = ShopOrderModel::get(Request::param("id"));
        $order->express_company = Request::param("express_company");
        $order->express_no      = Request::param("express_no");
        $order->status          = 2;
        if ($order->save()) {
            // 记录日志
            $log_content = "修改商城订单发货，订单ID：{$order->id}，单号：{$order->order_no}，价格：{$order->price}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

}